//
//  CZStatusPicktureView.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
// cell 可重用标示符
private let CZStatusPictureViewCellId = "CZStatusPictureViewCellId"

class CZStatusPicktureView: UICollectionView {
    // cell 间距 和 宽高
    private let margin: CGFloat = 5
    private let cellWH: CGFloat = 80
    //供外界赋值
    var pictureModel:[CZPictureModel]?{
       
        didSet{
//            countLabel.text = "\(pictureModel?.count ?? 0)"
            
            //得到size
            let size = getPictureSize(pictureModel?.count ?? 0)
            // 更新约束 因为cell 是复用的 但是pictureView的相对约束的对象没有发生改变
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            //刷新
            reloadData()
            
        }
        
    }
    
    //计算图片的SIZE
    func getPictureSize(count:Int)->CGSize{
        // 行数和列数
        let col = count == 4 ? 2:  count>3 ? 3:count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
        
        //计算宽高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*margin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*margin
        
        return CGSize(width: w,height: h)
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置UI视图
    private func setupUI(){
        backgroundColor = UIColor.clearColor()
//        addSubview(countLabel)
        
//        countLabel.snp_makeConstraints { (make) in
//            make.center.equalTo(self)
//        }
        
        
        
        //设置代理
        dataSource = self
        //注册Cell
        registerClass(CZStatusPicktureViewCell.self, forCellWithReuseIdentifier: CZStatusPictureViewCellId)
        //设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置item Size
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
      
        //设置水平间距
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
        
        
    }
    
    
    //懒加载控件
//    private lazy var countLabel:UILabel = UILabel(textColor: UIColor.blackColor(), textSize: 15)
    
}


extension CZStatusPicktureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureModel?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZStatusPictureViewCellId, forIndexPath: indexPath) as! CZStatusPicktureViewCell
        //给cell赋值
        cell.picUrl = pictureModel![indexPath.item]
        
        return cell
    }
}




//自定义cell
class CZStatusPicktureViewCell:UICollectionViewCell{
    
    //提供一个属性供外界赋值
    var picUrl:CZPictureModel?{
        
        didSet{
            
            placeImageView.cz_setImageWithString(picUrl?.thumbnail_pic, playholderImageName: "avatar_default")
            
            //判断图片是否是gif图片
            if let pic = picUrl?.thumbnail_pic where pic.hasSuffix(".gif") {
                // 显示(cell 复用问题)
                gifImageView.hidden = false
            }else{
                gifImageView.hidden = true
            }
            
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        //添加控件
        contentView.addSubview(placeImageView)
        contentView.addSubview(gifImageView)
        
        //设置约束
        placeImageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) in
            make.trailing.bottom.equalTo(contentView)
        }
        
    }
    
    
    
    //懒加载控件
    private lazy var placeImageView:UIImageView = {
       let image = UIImageView(imageName: "avatar_default")
        image.contentMode = .ScaleAspectFill
        image.clipsToBounds = true
        
        return image
    }()
    
    //懒加载gif图片
    private lazy var gifImageView:UIImageView = UIImageView(imageName: "timeline_image_gif")
    
}











